import React, { useState } from 'react';
import { Button, Modal, Form, Input } from 'antd';
import TRNotification from '#/utils/notification.js';
import { HttpCode } from '#/utils/contacts.js';
import planIcon from '@/assets/img/plan_icon.png';
import type { IApplyType } from '../../type';
import styles from './index.less';

interface IProps {
  onPress: (data: Record<string, any>) => void;
  onOk: (params: IApplyType) => void;
}

const ApplyModalComponent = (props: IProps) => {
  const { onOk, onPress } = props;
  const [form] = Form.useForm();
  const [visible, setVisible] = useState<boolean>(true);
  const [loading, setLoading] = useState<boolean>(false);

  /**
   * 取消按钮
   */
  const _onCancel = () => {
    setVisible(false);
    onPress({ index: 0 });
  };

  const _onOk = async () => {
    form &&
      form.validateFields().then(async (value) => {
        setLoading(true);
        const res: any = await onOk(value);
        if (res && res.statusCode === HttpCode.SUCCESS) {
          setVisible(false);
          onPress({ index: 1, statusCode: HttpCode.SUCCESS });
        }
        setLoading(false);
      });
  };

  const footerRender = () => {
    return [
      <Button key="dtest1" onClick={_onCancel} loading={loading}>
        取消
      </Button>,
      <Button key="dtest2" type="primary" onClick={_onOk} loading={loading}>
        确定
      </Button>,
    ];
  };

  return (
    <Modal
      className={styles.modal}
      centered
      maskClosable={false}
      visible={visible}
      title={
        <div className={styles.titleDiv}>
          <img src={planIcon} className={styles.titleLogo} />
          <span className={styles.title}>授权申请</span>
        </div>
      }
      onCancel={_onCancel}
      footer={footerRender()}
    >
      <div className={styles.container}>
        <Form
          className={styles.form}
          form={form}
          name="apply"
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 20 }}
          colon={false}
          labelAlign="left"
          autoComplete="off"
          requiredMark={false}
        >
          <Form.Item
            name="stationId"
            label="场站ID"
            rules={[
              { pattern: new RegExp(/^[1-9]\d*$/, 'g'), message: '请输入数字' },
              { required: true, message: '请填写场站ID' },
            ]}
          >
            <Input maxLength={10} placeholder="请填写场站ID" />
          </Form.Item>
          <Form.Item
            name="remark"
            label="备注"
            rules={[{ max: 100, message: '备注不可超过100个字符' }]}
          >
            <Input.TextArea maxLength={100} placeholder="请填写备注" />
          </Form.Item>
        </Form>
      </div>
    </Modal>
  );
};

class ApplyModal {
  __key__ = '';

  show = (props: any) => {
    return new Promise((resolve) => {
      if (this.__key__ !== '') return;
      this.__key__ = String(Date.now());
      TRNotification.add({
        key: this.__key__,
        content: (
          <ApplyModalComponent
            {...props}
            onPress={(result: any) => {
              TRNotification.remove(this.__key__);
              this.__key__ = '';
              resolve(result);
            }}
          />
        ),
        duration: null,
        dismiss: this.dismiss,
      });
    });
  };

  dismiss = () => {
    if (this.__key__.length > 0) {
      TRNotification.remove(this.__key__);
      this.__key__ = '';
    }
  };
}

export default new ApplyModal();
